package org.nmgyjt.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.nmgyjt.springboot.entity.area.Area;
import org.nmgyjt.springboot.entity.rescueTeam.RescueTeam;

import java.util.List;
import java.util.Map;

@Mapper
public interface RescueTeamMapper extends BaseMapper<RescueTeam> {
    @Select("SELECT\n" +
            "\tDISTINCT a.org_name,\n" +
            "\text.source_type,\n" +
            "\text.source_level,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.address,\n" +
            "\tr.team_num,\n" +
            "\tr.equipage_num \n" +
            "FROM\n" +
            "\tt_cs_system_admin a \n" +
            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN rescue_team_info r ON ext.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000010' " +
            " AND a.superior_org=#{createdBy, jdbcType=VARCHAR} "+
            " AND r.state <> '1' AND ext.org_id is not null OR ext.source_level LIKE #{orgId, jdbcType=VARCHAR}")
//            "\tAND ext.superior_org_id=#{depId, jdbcType=VARCHAR}")
    List<Map<String,Object>> statisticRescueTeamInfoCount(@Param("createdBy") String createdBy,@Param("orgId") String orgId);

    @Select("SELECT a.* FROM (SELECT\n" +
            "\tDISTINCT a.org_name,\n" +
            "\tr.team_type source_type,\n" +
            "\tr.leveled source_level,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.state,\n" +
            "\tr.address,\n" +
            "\tr.team_num,\n" +
            "\tr.equipage_num, \n" +
            "\tr.dep_id,ext.org_id\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN rescue_team_info r ON ext.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000010'    ${sql}     ) a WHERE a.org_id is not null ${sql2}")
    List<Map<String,Object>> statisticRescueTeam(String sql,String sql2);

    @Select("SELECT count(1) FROM (SELECT \n" +
            "\tDISTINCT a.org_name,\n" +
            "\text.source_type\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN rescue_team_info r ON ext.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000010' " +
            " AND a.superior_org=#{createdBy, jdbcType=VARCHAR} "+
            " AND r.state = '0' AND ext.org_id is not null OR ext.source_level LIKE #{orgId, jdbcType=VARCHAR} ) a")
    int statisticRescueTeamCountDoneStatus(@Param("createdBy") String createdBy,@Param("orgId") String orgId);

    @Select("SELECT count(1) FROM (SELECT\n" +
//            "\tDISTINCT a.org_name,\n" +
            "\text.source_type\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN rescue_team_info r ON ext.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\t a.menu_id='1000000010'  AND r.state <> '1' AND ext.org_id is not null \n" +
            "\t AND a.superior_org=#{createdBy, jdbcType=VARCHAR}\n" +
            "\t OR ext.source_level LIKE #{orgId, jdbcType=VARCHAR}\n" +
            "\tgroup by ext.source_type) a")
    Integer statisticRescueTeamCount(@Param("createdBy") String createdBy,@Param("orgId") String orgId);

    @Update("UPDATE rescue_team_info a SET a.state '1' WHERE a.dep_id = #{depId,jdbcType=VARCHAR}")
    Integer deleteByDepId(@Param("depId") String depId);
}
